<template>
  <div>
    <van-cell title="搜索结果" />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push(`/article/${item.art_id}`)"
      />
    </van-list>
  </div>
</template>

<script>
import { searchResult } from '@/api/user.js'
export default {
  name: 'SearchResult',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: {
        page: 1,
        per_page: 10,
        q: this.value
      },
      total: 0
    }
  },
  methods: {
    async onLoad () {
      const { data } = await searchResult(this.page)
      this.list = [...this.list, ...data.results]
      console.log(data)
      this.total = data.total_count
      this.loading = false
      if (this.total > this.list.length) {
        this.page.page++
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style></style>
